import json
from flask import request, jsonify
from src import common
from utils.initApp import default_response

from . import user
from .service import UserService


@user.route('/listAll', methods=['GET'])
def list_all():

    result = userService.list_all()
    response = default_response.construct_response(result)
    return response


@user.route('/avatar', methods=['GET'])
def avator():
    token = common.get_token()

    values = dict()
    values['userId'] = token['userId']

    result = userService.avatar(values)
    response = default_response.construct_response(result)
    return response


@user.route('/get/<user_id>', methods=['GET'])
def get(user_id):

    values = dict()
    values["userId"] = user_id

    result = userService.get(values)
    response = default_response.construct_response(result)
    return response


@user.route('/update', methods=['POST'])
def update():
    data = json.loads(request.get_data(as_text=True))
    token = common.get_token()

    values = dict()
    values['userId'] = token["userId"]
    values['email'] = data["email"]
    values['phone'] = data["phone"]
    values['gender'] = data["gender"]
    values['sign'] = data["sign"]

    result = userService.update(values)
    response = default_response.construct_response(result)
    return response


@user.route('/reset', methods=['POST'])
def reset():
    data = json.loads(request.get_data(as_text=True))
    token = common.get_token()

    values = dict()
    values['userId'] = token["userId"]
    values['oldPassword'] = data["oldPassword"]
    values['newPassword'] = data["newPassword"]

    result, msg, code = userService.reset(values)
    response = default_response.construct_response(result, msg, code)
    return response


userService = UserService(
    ['userName', 'userId', 'email', 'phone', 'gender', 'sign'])
